
public class Map 
{
public El head = null;
	
	public void printMap()
	{
		if(head == null)System.out.println("Error: Map is empty!");
		else
		{
			El cur = head;
			System.out.print("Key list: ");
			while(cur.next!=null)
			{
				System.out.print(cur.key+" ");
				cur = cur.next;
				}
			System.out.println();
		}
	}
	
 	public void Add(El elem)
 	{
		if(head== null)head = elem;
		else
		{
			if(head.next == null)
			{
				if(head.key.compareTo(elem.key)<0)
				{
					head.next = elem;
					return;
				}
				if(head.key.compareTo(elem.key)>0)
				{
					elem.next = head;
					head = elem;
					return;
				}
				if(head.key.compareTo(elem.key)==0)
				{
					System.out.println("Error: key '"+head.key+"' already used! ('"+head.value+"','"+elem.value+"')");
					return;
				}
			}
			if(head.key.compareTo(elem.key)<0)
			{
				elem.next = head.next;
				head.next = elem;
				return;
			}
			if(head.key.compareTo(elem.key)>0)
			{
				elem.next = head;
				head = elem;
				return;
			}
			if(head.key.compareTo(elem.key)==0)
			{
				System.out.println("Error: key '"+head.key+"' already used! ('"+head.value+"','"+elem.value+"')");
				return;
			}
			El cur = head;
			while(cur.next!=null)
			{
				if(cur.key.compareTo(elem.key)>0 || elem.key.compareTo(cur.next.key)<0)
				{
					elem.next=cur.next;
					cur.next=elem;
					return;
				}
				cur = cur.next;
			}
			cur.next = elem;
		}
	}	
	public String Get(String key)
	{
		if(head==null)
		{
			System.out.println("Error: Map is empty!");
			return null;
		}
		else
		{
			if(head.key == key)return head.value;
			else
			{
				El cur = head;
				while(cur.next!=null)
				{
					if(cur.key == key)return cur.value;
					cur = cur.next;
				}
				System.out.println("Error: there isn't record with the key '"+key+"'!");
				return null;
			}
		}
	}
	
	public void Delete_Key(String key)
	{
		if(head.key == key)head = head.next;
		else
		{
			El cur = head;
			while(cur.next!=null)
			{
				if(cur.next.key == key)
				{
					cur.next = cur.next.next;
					return;
				}
				cur = cur.next;
			}
		}
	}
	
	public void Delete_Value(String value)
	{
		Boolean flag=false;
		if(head == null) return;
		if(head.value == value)
		{
			if(head.next!=null)head = head.next;
			else
			{
				head = null;
				return;
			}
			while(head.value==value)
				if(head.next!=null) head = head.next;
				else
				{
					head = null;
					return;
				}
		};
		El cur = head;
		while(cur.next!=null)
		{
			if(cur.next.value == value)
			{
				cur.next = cur.next.next;
				flag=true;
			}
			if(flag==false) cur = cur.next;
			else flag=false;
		}
	}
	
	public void Delete_KeyAndValue(String key, String value)
	{
		if(head.key == key && head.value == value)head = head.next;
		else
		{
			El cur = head;
			while(cur.next!=null)
			{
				if(cur.next.key == key && cur.next.value == value)
				{
					cur.next = cur.next.next;
					return;
				}
				cur = cur.next;
			}
		}
	}
	
	public String toString()
	{
		StringBuffer sb = new StringBuffer();
		El current = head;
		sb.append("KEY\t");
		sb.append("VALUE\n");
		while (current != null)
		{
			sb.append(current.key + "\t");
			sb.append(current.value + "\n");
			current = current.next;
		}
		return sb.toString();
	}
}
